import { QueryClient, useMutation } from "@tanstack/react-query";
import { AxiosError } from "axios";
import { CompletedTaskService } from "@/shared/api/api.completedTask";

export function useCompleteLesson(lessonId:number) {
    const queryClient = new QueryClient

    const completeLessonMutation = useMutation(
        {
            mutationFn: CompletedTaskService.create,
            onError: (error: (AxiosError<{ message: string }>)) => error,
        }, queryClient
    );
    const errorMessage = completeLessonMutation.error?.response?.data.message

    return {
        errorMessage,
        isSuccess: completeLessonMutation.isSuccess,
        handleSubmit: () => {
            completeLessonMutation.mutate({lessonId})
        }
    };
}